package com.smh.springboottest.config;

import cn.itcast.pojo.Country;
import cn.itcast.pojo.Province;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * @author shiminghui
 * @date 2024/9/24 14:04
 * @description: TODO
 */
//@Configuration
public class TestConfiguration {
    @Bean//默认方法名是备案对象的名字
    @ConditionalOnProperty(prefix = "country", name = {"name", "system"}) //当配置文件中有值时，才会创建这个bean
    public Country country(@Value("${country.name}") String name, @Value("${country.system}") String system) {
        Country country = new Country();
        country.setName(name);
        country.setSystem(system);
        return country;
    }

    @Bean
//    @ConditionalOnMissingBean(Country.class) //当没有这个Country时，才会创建这个bean
    @ConditionalOnClass(DispatcherServlet.class) //如果当前类加载了DispatcherServlet，才会创建这个bean
    //相当于只有你引入了web的起步依赖,才会加载这个bean
    public Province province() {
//        System.out.println("SADasd" + country);
        return new Province();
    }
}
